import axios from 'axios'
import store from '@/store'
import { getToken, getTokenByName } from '@/utils/auth'
import { Message, MessageBox } from 'element-ui'
let service_hxy = axios.create({
  baseURL:process.env['VUE_APP_BASE_HXY_API'], // url = base url + request url
  // baseURL:'/hxy_api', // url = base url + request url
  withCredentials: false, // send cookies when cross-domain requests
  timeout: 30000,// request timeout
  headers: {
    'Content-Type': "application/json;charset=utf-8",
  },

})
// service_hxy.defaults.withCredentials = false
// request interceptor
service_hxy.interceptors.request.use(
  config => {
    // do something before request is sent

    if (store.getters.token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation

      // config.headers['X-Token'] = getToken()

    }
    return config
  },
  error => {
    // do something with request error
    console.log("请求报错："+error) // for debug
    // return Promise.reject(error)//直接报错提示
    return error
  }
)
// response interceptor
service_hxy.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {

    // console.log("Response==="+JSON.stringify(response))
    return response

    // const res = response.data
    // let codeInt = parseInt(res.code)
    // if the custom code is not 20000, it is judged as an error.
    // if (codeInt !==20000 ) {
    //   Message({
    //     message: res.message || 'Error',
    //     type: 'error',
    //     duration: 5 * 1000
    //   })
    //
    //   // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
    //   if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
    //     // to re-login
    //     MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
    //       confirmButtonText: 'Re-Login',
    //       cancelButtonText: 'Cancel',
    //       type: 'warning'
    //     }).then(() => {
    //       store.dispatch('user/resetToken').then(() => {
    //         location.reload()
    //       })
    //     })
    //   }
    //   return Promise.reject(new Error(res.msg || 'Error'))
    // } else {
    //   return res
    // }
  },
  error => {
    console.log( error) // for debug
    // Message({
    //   message: error.message,
    //   type: 'error',
    //   duration: 5 * 10000
    // })
    // return Promise.reject(error)
    return -1
  }
)

/**
 * 同步用户及用户绑定码(新增用户)
 * @param data
 * @returns {AxiosPromise}
 */
export  function addConsumerHXY(data){

  // //这个是测试数据
  //  data = {
  //    role_ids:[557],
  //    structure_ids:data.structure_ids,//XX年级XX版
  //    account:store.getters.name,
  //    phone:"11111111111",
  //    realname:"cxy",
  //    password:"111111",
  //    bindcode:"bindcode",
  //    pay_status:0,
  //    expire:20201212,
  //    iptv_bindcode:"test",
  // }

  return service_hxy({
    url:'/userapi/v1/membersyncbind',
    method:'post',
    headers:{
      Authorization:'Bearer '+getTokenByName("token_hxy")
    },
    data
  })
}


//用户登录，获取token用于后续需要此参数的接口、
export function loginHXY(data){

  // data=
  // {
  //   "account":"testklmb",
  //   "password":"111111",
  //   "tenant_id":"ZIB7EZ",
  //   "bindcode":"testklmb"
  // }


  // console.log("222="+process.env['VUE_APP_BASE_HXY_API'])

  return service_hxy({
    url: '/userapi/v1/loginhb',
    method:'post',

    data
  })


}


/**
 * 创建默认的组织结构
 * @param data
 * @returns {AxiosPromise}
 */
export  function addStructure(data){

  return service_hxy({
    url:'/userapi/v1/structure',
    method:'post',
    headers:{
      Authorization:'Bearer '+getTokenByName("token_hxy")
    },
    data
  })
}

/**
 * 获取租户默认的组织结构
 * @returns {AxiosPromise}
 */
export  function  getStructure(){

  return service_hxy({
    url:'/userapi/v1/structuretree',
    method:'get',
    headers:{
      Authorization:'Bearer '+getTokenByName("token_hxy")
    },
  })
}


/**
 * 获取租户默认的组织结构
 * @returns {AxiosPromise}
 */
export  function  getRoldsHXY(){

  return service_hxy({
    url:'/userapi/v1/role',
    method:'get',
    headers:{
      Authorization:'Bearer '+getTokenByName("token_hxy")
    },
  })
}

/***
 * 修改用户付费状态
 * @param bindcode
 * @param data
 * @returns {AxiosPromise}
 */
export  function  updatePayStatus(bindcode,data){
  return service_hxy({
    url:`/userapi/v1/memberupdate/${bindcode}`,
    method:'post',
    data,
    headers:{
      Authorization:'Bearer '+getTokenByName("token_hxy")
    },
  })
}

/**
 * 删除管理员、学生、教师
 * @param bindcode
 * @returns {AxiosPromise}
 */
export  function  delConsumerHXY(bindcode){

  return service_hxy({
    url:`/userapi/v1/memberdel/${bindcode}`,
    method:'delete',
    // data,
    headers:{
      Authorization:'Bearer '+getTokenByName("token_hxy")
    },
  })

}
